Shader "YoFi/Spine/Skeleton" {
	Properties{
		[Header(Custom)]
		[Toggle]_NonTint("Non Tint",float) = 1
		_Color("Color",Color) = (1,1,1,1)
		_FillColor("Fill color(fx)",Color) = (1,1,1,0.5)
		[Enum(Default,0,Add,1,Multiplica,2,Blend,3,Alpha,4,Gray,5,Dark,6)] _Fillblend("Blend Value(Fill,fx)",float) = 0

		_Saturation("Saturation", Range(0,1)) = 1.0

		
		// [HideInInspector] _custom_time("当前_Time 时间(需传入)",float) = 0
		[Space(5)][Header(Source)]
		_Cutoff("Shadow alpha cutoff", Range(0,1)) = 0.1
		[NoScaleOffset] _MainTex("Main Texture", 2D) = "black" {}
		[Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 1
		[HideInInspector] _StencilRef("Stencil Reference", float) = 1.0
		[HideInInspector][Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Comparison", float) = 8 // Set to Always as default
	

		[Header(Custom Bloom)]        
        [Toggle]_CBLocalBanBool("单独关闭Bloom",float) = 1
        _CBLocalThreshhold("CB Threshhold",range(0.0,1.0)) = 1
        _CBLocalColor("CB Color",Color) = (1,1,1,1)
        _CBLocalLight("CB 亮度",float) = 1
		
		[Header(Dissolve)]
		[Toggle] _Dissolve_ON("Dissolve ON", Float) = 0
		_DissolveFactor("Dissolve Factor", Range( 0 , 1)) = 0
		_SpineHeight("Spine Height", Float) = 3
		_SpineBottom("Spine Bottom", Float) = 0
		_RangFade("Rang Fade", Range( 0, 1)) = 0.3
		_DissolveTex("Dissolve Tex", 2D) = "white" {}
		[HDR]_DissolveColor("Dissolve Color", Color) = (1,1,1,1)
        _DissolveSoft("Dissolve Soft", Range( 0 , 0.5)) = 0.1
        _DissolveWide("Dissolve Wide", Range( 0 , 1)) = 0.05
        _DissolveUSpeed("Dissolve U Speed", Float) = 0.0
        _DissolveVSpeed("Dissolve V Speed", Float) = 0.5
	}

	SubShader{
		Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" "YFCB" = "Spine_Skeleton"}

		Fog { Mode Off }
		Cull Off
		ZWrite Off
		ZTest Always
		Blend One OneMinusSrcAlpha
		Lighting Off

		Stencil {
			Ref[_StencilRef]
			Comp[_StencilComp]
			Pass Keep
		}

		Pass {
			Name "Normal"

			CGPROGRAM
			#pragma shader_feature _ _STRAIGHT_ALPHA_INPUT

			#pragma multi_compile _ _ON_REFLECT

			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"			
			#include "..\Common\Included\YFEffectCGForYFPost.cginc"

			sampler2D _MainTex;
			//Dite change
			float4 _FillColor;
			float _Fillblend;
			float4 _Color;
			float _Saturation;


			//溶解相关
			float _SpineHeight;
			float _SpineBottom;
			float _Dissolve_ON;
			float _DissolveFactor;
			float _DissolveSoft;
			float _DissolveWide;
			float _RangFade;
			float _DissolveUSpeed;
			float _DissolveVSpeed;
			float4 _DissolveColor;
			float4 _DissolveTex_ST;
			sampler2D _DissolveTex;


			//脚本设置的全局参数
			float4 _ReflectionDate;
			half4 _GlobalSpineTint;
			float _NonTint;

			void Unity_Remap_float(float In, float2 InMinMax, float2 OutMinMax, out float Out)
			{
			    Out = OutMinMax.x + (In - InMinMax.x) * (OutMinMax.y - OutMinMax.x) / (InMinMax.y - InMinMax.x);
			}

			// /*if(Delytime >= 0){
				// 	this.m_Material.SetFloat("_custom_time", Time.timeSinceLevelLoad + Delytime);
				// }else{
				// 	this.m_Material.SetFloat("_custom_time", -1);
			// }*/
			// float _custom_time; //(max(0,_Time.y - _custom_time) * -_speed );

			inline float4 blendmodel(in float4 finalcol) { 
				float4 fxcol = _FillColor;
				if (_Fillblend == 1) { //Add
					return float4(fxcol.rgb + finalcol.rgb, finalcol.a);
				}
				else if (_Fillblend == 2) //Multiplica
				{
					return float4(fxcol.rgb * finalcol.rgb, finalcol.a);
				}
				else if (_Fillblend == 3) { //blend
					return  float4(lerp(finalcol.rgb, fxcol.rgb, fxcol.a), finalcol.a);
				}
				else if (_Fillblend == 4) { //Alpha
					return  float4(finalcol.rgb, saturate(fxcol.a * finalcol.a));
				}
				else if (_Fillblend == 5) { // Gray
					fixed3 v = dot(finalcol.rgb, unity_ColorSpaceGrey) * _FillColor.a;
					return  float4(v, finalcol.a);
				}
				else if (_Fillblend == 6) { // Dark
					fixed4 darkColor = fixed4(0.55,0.55,0.55,1);
					fixed3 v = float4(darkColor.rgb * finalcol.rgb, finalcol.a);
					return  float4(v, finalcol.a);
				}
				else { //Default 
					return finalcol;
				}
			}
			

			inline float4 PMAGammaToTargetSpace(float4 gammaPMAColor) {
				#if UNITY_COLORSPACE_GAMMA
					return gammaPMAColor;
				#else
					return gammaPMAColor.a == 0 ?
					float4(GammaToLinearSpace(gammaPMAColor.rgb), gammaPMAColor.a) :
					float4(GammaToLinearSpace(gammaPMAColor.rgb / gammaPMAColor.a) * gammaPMAColor.a, gammaPMAColor.a);
				#endif
			}

			struct VertexInput {
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float4 vertexColor : COLOR;
			};

			struct VertexOutput {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				//float3 clannel : TEXCOORD1;
				float3 positionOS  : TEXCOORD1;
				#ifdef _ON_REFLECT
				float3 pos_ws : TEXCOORD2;
				#endif
				
				float4 vertexColor : COLOR;
			};

			VertexOutput vert(VertexInput v) {
				VertexOutput o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				o.vertexColor = v.vertexColor;
				o.positionOS = v.vertex.xyz;

				#ifdef _ON_REFLECT
				o.pos_ws = mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1.0)).xyz;
				#endif
				return o;
			}

			float4 frag(VertexOutput i) : SV_Target{
				float4 finalcol = 0;
				float4 texColor = tex2D(_MainTex, i.uv.xy);
				
				finalcol = texColor;
				// 获取特效使用的全覆盖颜色


				half dissolveAlpha_final = 1.0;
                half3 mainColor_final_useDissolve = finalcol.rgb;
				UNITY_BRANCH
				if (_Dissolve_ON)
				{
					half2 uv_DissolveTex = i.positionOS.xy * _DissolveTex_ST.xy + _DissolveTex_ST.zw;
					half2 uv_Dissolve_speed = (half2(_DissolveUSpeed , _DissolveVSpeed));
					uv_DissolveTex -= frac( _Time.y * uv_Dissolve_speed);
					half dissolveTex = tex2D(_DissolveTex,uv_DissolveTex).r;
					

					
					float rampValue = 1;
					Unity_Remap_float(i.positionOS.y,float2(_SpineBottom,_SpineHeight),float2(0,1), rampValue);
					rampValue = 1 -rampValue;
					float fade  = 0;
					Unity_Remap_float(_DissolveFactor,float2(0,1),float2(-_RangFade,1), fade);
					float geadient = (rampValue - fade)/ _RangFade * 2;

					float value = geadient - dissolveTex;
					float softvalue = smoothstep(_DissolveSoft,0.5,value);
					float edge = distance(value,_DissolveSoft);
					edge = saturate(1 - edge/_DissolveWide);


					half3 colorValue = finalcol.rgb * _DissolveColor.rgb;
					colorValue = lerp(finalcol.rgb,colorValue,edge);
					half Alphavalue = softvalue;
					

					mainColor_final_useDissolve = colorValue;
					dissolveAlpha_final = Alphavalue;
				}
				finalcol.rgb = mainColor_final_useDissolve;
				finalcol.a *= dissolveAlpha_final;

				
				finalcol = blendmodel(finalcol);
				finalcol *= lerp(half4(1,1,1,1),_GlobalSpineTint,step(_NonTint,0.001));
				finalcol *= _Color;
				
				#ifdef _ON_REFLECT
				half pos_y_plane = i.pos_ws.y + _ReflectionDate.x;
				half soft = 1 - saturate( pow(pos_y_plane , _ReflectionDate.y) + _ReflectionDate.z);
				finalcol.rgb *= soft;
				#endif
				
				//saturation饱和度：首先根据公式计算同等亮度情况下饱和度最低的值：
				half gray = 0.2125 * finalcol.r + 0.7154 * finalcol.g + 0.0721 * finalcol.b;
				half3 grayColor = half3(gray, gray, gray);
				//根据Saturation在饱和度最低的图像和原图之间差值
				finalcol.rgb = lerp(grayColor, finalcol.rgb, _Saturation);
				

				#if defined(_STRAIGHT_ALPHA_INPUT)
					finalcol.rgb *= finalcol.a;
				#endif
				
				finalcol *= i.vertexColor;
				
				return BloomOutfrag(saturate(finalcol));
			}
			ENDCG
		}
	}
	//CustomEditor "SpineShaderWithOutlineGUI"
}
